package com.manxi.service.impl;

import com.alibaba.fastjson.serializer.BeanContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manxi.constant.MessageConstant;
import com.manxi.constant.PasswordConstant;
import com.manxi.constant.StatusConstant;
import com.manxi.context.BaseContext;
import com.manxi.dto.EditPasswordDTO;
import com.manxi.dto.EmployeeDTO;
import com.manxi.dto.EmployeeLoginDTO;
import com.manxi.dto.EmployeePageQueryDTO;
import com.manxi.entity.Category;
import com.manxi.entity.Employee;
import com.manxi.exception.AccountLockedException;
import com.manxi.exception.AccountNotFoundException;
import com.manxi.exception.PasswordErrorException;
import com.manxi.mapper.EmployeeMapper;
import com.manxi.result.PageResult;
import com.manxi.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码对比
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 修改密码
     *
     * @return
     */
    public void editPassword(EditPasswordDTO editPasswordDTO) {

        Long id = BaseContext.getCurrentId();
        Employee employee = employeeMapper.getById(id);
        String oldPassword = editPasswordDTO.getOldPassword();
        String newPassword = editPasswordDTO.getNewPassword();

        //密码加密对比
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());

        if (!employee.getPassword().equals(oldPassword)){
            //原密码错误
            throw new PasswordErrorException(MessageConstant.OLDPASSWORD_ERROR);
        }

        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        //修改密码
        employeeMapper.editPassword(id,newPassword);
    }


    /**
     * 新增员工
     * @param employeeDTO
     */
    public void save(EmployeeDTO employeeDTO) {
        //创建Employee对象
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);
        //设置账号状态,默认正常状态 1表示正常 0表示锁定
        employee.setStatus(StatusConstant.ENABLE);
        //设置密码，默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //调用Dao层插入数据
        employeeMapper.insert(employee);
    }


    /**
     * 分页查询员工
     * @param employeePageQueryDTO
     * @return
     */
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        //设置分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        //返回查询结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 更改员工状态（启用禁用）
     * @param status
     * @param id
     */
    public void updateEmployeeStatus(Integer status, long id) {
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();

        employeeMapper.updateEmployee(employee);

    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    public Employee getById(long id) {
        Employee employee = employeeMapper.getById(id);
        //设置密码不可见
        employee.setPassword("******");
        return employee;
    }


    /**
     * 更改员工信息
     * @param employeeDTO
     */
    public void updateEmployeeInfo(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.updateEmployee(employee);
    }

    /**
     * 根据id删除员工
     *
     * @param id
     * @return
     */
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }



}
